﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Management;
using Microsoft.VisualBasic.Devices;
using System.Diagnostics;

namespace Tools
{
    public partial class Tools : Form
    {
        public Tools()
        {
            InitializeComponent();
            this.Text = Computer.SystemVersionName;
        }

        private void ServicesButtonClick(object sender, EventArgs e)
        {
            Process Process = new Process();
            Process.StartInfo.FileName = "services.msc";
            Process.Start();

            //Process p = new Process();
            ////设置要启动的应用程序
            //p.StartInfo.FileName = "cmd.exe";
            ////是否使用操作系统shell启动
            //p.StartInfo.UseShellExecute = false;
            //// 接受来自调用程序的输入信息
            //p.StartInfo.RedirectStandardInput = true;
            //// 设置不输出信息
            //p.StartInfo.RedirectStandardOutput = false;
            //// 输出错误
            //p.StartInfo.RedirectStandardError = true;
            ////不显示程序窗口
            //p.StartInfo.CreateNoWindow = true;

            //try
            //{
            //    //启动程序
            //    p.Start();
            //    //向cmd窗口发送输入信息
            //    p.StandardInput.WriteLine("services.msc");
            //    //自动处理缓存数据　　　　　　　　　　　　　　 
            //    p.StandardInput.AutoFlush = true;
            //    //退出命令窗口
            //    p.StandardInput.WriteLine("exit");
            //    //等待程序执行完退出进程
            //    p.WaitForExit();
            //}
            //catch (Exception ex)
            //{
            //    throw ex;
            //}
            //finally
            //{
            //    if (p != null)
            //    {
            //        p.Close();
            //    }
            //}
        }

        private void RegeditButtonClick(object sender, EventArgs e)
        {
            Process Process = new Process();
            Process.StartInfo.FileName = "regedit";
            Process.Start();
        }

        private void GpeditButtonClick(object sender, EventArgs e)
        {
            Process Process = new Process();
            Process.StartInfo.FileName = "gpedit.msc";
            Process.Start();
        }

        private void UserButtonClick(object sender, EventArgs e)
        {
            Process Process = new Process();
            Process.StartInfo.FileName = "lusrmgr.msc";
            Process.Start();
        }

        private void FirewallButtonClick(object sender, EventArgs e)
        {
            Process Process = new Process();
            Process.StartInfo.FileName = "wf.msc";
            Process.Start();
        }
    }

    public class Computer
    {
        public static string CpuID; //1.cpu序列号
        public static string MacAddress; //2.mac序列号
        public static string DiskID; //3.硬盘id
        public static string IpAddress;//4.ip地址
        public static string LoginUserName;//5.登录用户名
        public static string ComputerName;//6.计算机名
        public static string SystemType;//7.系统类型
        public static string TotalPhysicalMemory; //8.内存量 单位：M
        public static string SystemVersionName;//9.操作系统版本

        static Computer()
        {
            CpuID = GetCpuID();
            MacAddress = GetMacAddress();
            DiskID = GetDiskID();
            IpAddress = GetIPAddress();
            LoginUserName = GetUserName();
            SystemType = GetSystemType();
            TotalPhysicalMemory = GetTotalPhysicalMemory();
            ComputerName = GetComputerName();
            SystemVersionName = GetSystemVersionName();
        }
        //1.获取CPU序列号代码 

        static string GetCpuID()
        {
            try
            {
                string cpuInfo = "";//cpu序列号 
                ManagementClass mc = new ManagementClass("Win32_Processor");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                mc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        //2.获取网卡硬件地址 

        static string GetMacAddress()
        {
            try
            {
                string mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        //3.获取硬盘ID 
        static string GetDiskID()
        {
            try
            {
                String HDid = "";
                ManagementClass mc = new ManagementClass("Win32_DiskDrive");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                mc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        //4.获取IP地址 
        static string GetIPAddress()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                    {
                        //st=mo["IpAddress"].ToString(); 
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IpAddress"].Value);
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }
        /// 5.操作系统的登录用户名 
        static string GetUserName()
        {
            try
            {
                string un = "";

                un = Environment.UserName;
                return un;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        //6.获取计算机名
        static string GetComputerName()
        {
            try
            {
                return System.Environment.MachineName;

            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        ///7 PC类型 
        static string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["SystemType"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }
        }

        ///8.物理内存        
        static string GetTotalPhysicalMemory()
        {
            try
            {
                string st = "";
                ManagementClass mc = new ManagementClass("Win32_ComputerSystem");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["TotalPhysicalMemory"].ToString();
                }
                moc = null;
                mc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
            finally
            {
            }

        }

        ///9.获取操作系统版本和位数
        static string GetSystemVersionName()
        {
            string OsVersionName = new ComputerInfo().OSFullName;
            string OsVersion;
            switch (IntPtr.Size)
            {
                case 8:
                    OsVersion = OsVersionName + " 64位";
                    break;

                case 4:
                    OsVersion = OsVersionName + " 86位";
                    break;

                default:
                    OsVersion = OsVersionName;
                    break;
            }
            return OsVersion;

        }

    }
}
